﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UnityEngine;

namespace Azuo_Tool
{
    public class U_Save
    {
        public static double U_Load_Double(String name)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            Debug.Log(path);
            if (File.Exists(path))
            {
                String contante = File.ReadAllText(path);
                return Convert.ToDouble(contante);
            }
            return 0;
        }

        public static void U_Save_Double(String name, double date)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            String out_s = date.ToString();

            if (!Directory.Exists(Application.streamingAssetsPath + @"/MapReady/"))
            {
                Directory.CreateDirectory(Application.streamingAssetsPath + @"/MapReady/");
            }
            File.WriteAllText(path, out_s);
        }

        public static int[] U_Load_Array_Int(String name)
        {

            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            Debug.Log(path);
            int[] temple = null;
            if (File.Exists(path))
            {
                string[] contante = File.ReadAllLines(path);
                for (int y = 0; y < contante.Length; y++)
                {
                    if (temple == null)
                    {
                        temple = new int[contante.Length];
                    }

                    try
                    {
                        temple[y] = Convert.ToInt32(contante[y]);
                    }
                    catch
                    {
                        throw new Exception(name + "格式有问题或者不存在");
                    }

                }
            }

            return temple;
        }

        public static bool[] U_Load_Array_Bool(String name)
        {

            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            Debug.Log(path);
            bool[] temple = null;
            if (File.Exists(path))
            {
                string[] contante = File.ReadAllLines(path);
                for (int y = 0; y < contante.Length; y++)
                {
                    if (temple == null)
                    {
                        temple = new bool[contante.Length];
                    }

                    try
                    {
                        temple[y] = Convert.ToBoolean(contante[y]);
                    }
                    catch
                    {
                        throw new Exception(name + "格式有问题或者不存在");
                    }

                }
            }

            return temple;
        }
        public static void U_Save_Array_Bool(String name, bool[] date)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            String out_s = "";

            if (!Directory.Exists(Application.streamingAssetsPath + @"/MapReady/"))
            {
                Directory.CreateDirectory(Application.streamingAssetsPath + @"/MapReady/");
            }

            for (int x = 0; x < date.Length; x++)
            {
                string line = "";
                line += (date[x]);
                out_s += (line + "\n");
            }

            Console.WriteLine(out_s);
            File.WriteAllText(path, out_s);
        }

        public static void U_Save_2Array_Int(String name, int[,] date)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            String out_s = "";
            for (int y = date.GetLength(1) - 1; y >= 0; y--)
            {
                String line = "";
                for (int x = 0; x < date.GetLength(0); x++)
                {
                    line += (date[x, y]) + ",";
                }
                out_s += line += "\n";
            }
            Console.WriteLine(out_s);
            File.WriteAllText(path, out_s);

        }

        public static int[,] U_Load_2Array_Int(String name)
        {

            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            Debug.Log(path);
            int[,] temple = null;
            if (File.Exists(path))
            {
                string[] contante = File.ReadAllLines(path);
                for (int y = 0; y < contante.Length; y++)
                {
                    String[] lings = contante[y].Split(',');
                    for (int x = 0; x < lings.Length - 1; x++)
                    {
                        if (temple == null)
                        {
                            temple = new int[lings.Length - 1, contante.Length];
                        }
                        try
                        {
                            temple[x, contante.Length - 1 - y] = Convert.ToInt32(lings[x]);
                        }
                        catch
                        {
                            throw new Exception(name + "格式有问题或者不存在");
                        }

                    }
                }
            }

            return temple;
        }
        public static void U_Save_Array_Long(String name, long[] date)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            String out_s = "";
            if (!Directory.Exists(Application.streamingAssetsPath + @"/MapReady/"))
            {
                Directory.CreateDirectory(Application.streamingAssetsPath + @"/MapReady/");
            }

            for (int x = 0; x < date.Length; x++)
            {
                string line = "";
                line += date[x];
                out_s += (line + "\n");
            }

            Console.WriteLine("save Lone in" + path);
            File.WriteAllText(path, out_s);
        }

        public static long[] U_Load_Array_Long(String name)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            Debug.Log(path);
            long[] temple = null;
            if (File.Exists(path))
            {
                string[] contante = File.ReadAllLines(path);
                for (int y = 0; y < contante.Length; y++)
                {
                    String lings = contante[y];
                    if (temple == null)
                    {
                        temple = new long[contante.Length];
                    }

                    try
                    {
                        temple[y] = Convert.ToInt64(lings);
                    }
                    catch
                    {
                        throw new Exception(name + "格式有问题或者不存在");
                    }

                }
            }

            return temple;
        }

        public static void U_Save_Array_Ve2(String name, Vector2[] date)
        {
            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            String out_s = "";

            if (!Directory.Exists(Application.streamingAssetsPath + @"/MapReady/"))
            {
                Directory.CreateDirectory(Application.streamingAssetsPath + @"/MapReady/");
            }

            for (int x = 0; x < date.Length; x++)
            {
                string line = "";
                line += (date[x].x) + "," + (date[x].y);
                out_s += (line + "\n");
            }


            Console.WriteLine(out_s);
            File.WriteAllText(path, out_s);

        }

        public static Vector2[] U_Load_Array_Ve2(String name)
        {

            String path = Application.streamingAssetsPath + @"/MapReady/" + name + ".txt";
            Debug.Log(path);
            Vector2[] temple = null;
            if (File.Exists(path))
            {
                string[] contante = File.ReadAllLines(path);
                for (int y = 0; y < contante.Length; y++)
                {
                    String[] lings = contante[y].Split(',');
                    if (temple == null)
                    {
                        temple = new Vector2[contante.Length];
                    }

                    try
                    {
                        temple[y] = new Vector2();
                        temple[y].x = Convert.ToSingle(lings[0]);
                        temple[y].y = Convert.ToSingle(lings[1]);
                    }
                    catch
                    {
                        throw new Exception(name + "格式有问题或者不存在");
                    }

                }
            }

            return temple;
        }

        public static string[] Get_names(String name)
        {
            List<string> temple = new List<string>();
            String path = Application.streamingAssetsPath + @"/" + name;
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] fis = di.GetFiles();
            for (int i = 0; i < fis.Length; i++)
            {
                string temple_name = fis[i].Name;
                string[] temple_names = temple_name.Split('.');
                string end_name = temple_names[temple_names.Length - 1];

                if (end_name == "txt")
                {
                    temple.Add(temple_names[0]);
                }

            }
            return temple.ToArray();
        }

        public static int Get_Int(String name)
        {
            String path = Application.streamingAssetsPath + @"/" + name;

            string str1 = File.ReadAllText(path);
            int result = Convert.ToInt32(str1);
            return result;
        }

        public static string[] Get_Strings(String name)
        {
            String path = Application.streamingAssetsPath + @"/" + name;

            string[] str1 = File.ReadAllLines(path);

            return str1;
        }

    }
}

